<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>编辑个人资料 - Creeps</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .form-container {
            background: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 {
            text-align: center;
            color: #333;
            margin-bottom: 30px;
            padding-bottom: 15px;
            border-bottom: 2px solid #eee;
        }
        .form-group {
            margin-bottom: 20px;
        }
        label {
            display: block;
            margin-bottom: 8px;
            font-weight: bold;
            color: #555;
        }
        input[type="text"],
        input[type="email"],
        input[type="date"],
        textarea {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 16px;
            box-sizing: border-box;
        }
        textarea {
            min-height: 120px;
            resize: vertical;
        }
        .avatar-preview {
            text-align: center;
            margin-bottom: 20px;
        }
        .avatar-preview img {
            width: 120px;
            height: 120px;
            border-radius: 50%;
            object-fit: cover;
            border: 4px solid #ddd;
        }
        .default-avatar {
            width: 120px;
            height: 120px;
            border-radius: 50%;
            background-color: #ddd;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 48px;
            color: #666;
            margin: 0 auto;
        }
        .form-section {
            background: #f8f9fa;
            padding: 20px;
            border-radius: 8px;
            margin-bottom: 30px;
        }
        .form-section h2 {
            margin-top: 0;
            color: #444;
            font-size: 20px;
            padding-bottom: 10px;
            border-bottom: 1px solid #ddd;
            margin-bottom: 20px;
        }
        .action-buttons {
            text-align: center;
            margin-top: 30px;
        }
        .btn {
            display: inline-block;
            padding: 12px 24px;
            margin: 0 10px;
            text-decoration: none;
            border-radius: 5px;
            font-weight: bold;
            transition: background-color 0.3s;
            border: none;
            cursor: pointer;
            font-size: 16px;
        }
        .btn-primary {
            background-color: #007bff;
            color: white;
        }
        .btn-primary:hover {
            background-color: #0056b3;
        }
        .btn-secondary {
            background-color: #6c757d;
            color: white;
        }
        .btn-secondary:hover {
            background-color: #545b62;
        }
        .nav-link {
            display: inline-block;
            margin-bottom: 25px;
            color: rgba(100, 150, 255, 0.9);
            text-decoration: none;
            font-weight: 600;
            font-size: 1.1rem;
            transition: all 0.3s ease;
            padding: 10px 15px;
            border-radius: 10px;
            background: rgba(255, 255, 255, 0.1);
            backdrop-filter: blur(5px);
        }
        .nav-link:hover {
            color: white;
            background: rgba(100, 150, 255, 0.3);
            transform: translateX(5px);
            text-decoration: none;
        }
        .errorlist {
            color: #dc3545;
            list-style-type: none;
            padding: 0;
            margin: 5px 0 0;
            font-size: 14px;
        }
        .messages {
            list-style: none;
            padding: 0;
            margin-bottom: 20px;
        }
        .messages .success {
            background-color: #d4edda;
            color: #155724;
            padding: 12px;
            border-radius: 5px;
            margin-bottom: 10px;
        }
    </style>
</head>
<body>
    <div class="form-container">
        <a href="{% url 'accounts:profile' %}" class="nav-link">← 返回个人资料</a>

        <h1>编辑个人资料</h1>

        {% if messages %}
        <ul class="messages">
            {% for message in messages %}
            <li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
            {% endfor %}
        </ul>
        {% endif %}

        <form method="post" enctype="multipart/form-data">
            {% csrf_token %}

            <div class="form-section">
                <h2>基本信息</h2>

                <div class="form-group">
                    <label for="{{ user_form.username.id_for_label }}">用户名</label>
                    {{ user_form.username }}
                    {{ user_form.username.errors }}
                </div>

                <div class="form-group">
                    <label for="{{ user_form.first_name.id_for_label }}">名</label>
                    {{ user_form.first_name }}
                    {{ user_form.first_name.errors }}
                </div>

                <div class="form-group">
                    <label for="{{ user_form.last_name.id_for_label }}">姓</label>
                    {{ user_form.last_name }}
                    {{ user_form.last_name.errors }}
                </div>

                <div class="form-group">
                    <label for="{{ user_form.email.id_for_label }}">电子邮箱</label>
                    {{ user_form.email }}
                    {{ user_form.email.errors }}
                </div>
            </div>

            <div class="form-section">
                <h2>个人资料</h2>

                <div class="avatar-preview">
                    {% if user.profile.avatar %}
                        <img src="{{ user.profile.avatar.url }}" alt="当前头像">
                    {% else %}
                        <div class="default-avatar">
                            {{ user.username|first|upper }}
                        </div>
                    {% endif %}
                </div>

                <div class="form-group">
                    <label for="{{ profile_form.avatar.id_for_label }}">更换头像</label>
                    {{ profile_form.avatar }}
                    {{ profile_form.avatar.errors }}
                    <div id="avatar-upload-preview" style="margin-top: 10px; display: none;">
                        <p>新头像预览：</p>
                        <img id="avatar-preview-img" src="#" alt="头像预览" style="width: 120px; height: 120px; border-radius: 50%; object-fit: cover; border: 4px solid #ddd;">
                    </div>
                </div>

                <script>
                    document.addEventListener('DOMContentLoaded', function() {
                        const avatarInput = document.getElementById('{{ profile_form.avatar.id_for_label }}');
                        const previewContainer = document.getElementById('avatar-upload-preview');
                        const previewImg = document.getElementById('avatar-preview-img');

                        avatarInput.addEventListener('change', function() {
                            if (this.files && this.files[0]) {
                                const reader = new FileReader();

                                reader.onload = function(e) {
                                    previewImg.src = e.target.result;
                                    previewContainer.style.display = 'block';
                                }

                                reader.readAsDataURL(this.files[0]);
                            }
                        });
                    });
                </script>

                <div class="form-group">
                    <label for="{{ profile_form.bio.id_for_label }}">个人简介</label>
                    {{ profile_form.bio }}
                    {{ profile_form.bio.errors }}
                </div>

                <div class="form-group">
                    <label for="{{ profile_form.location.id_for_label }}">所在地</label>
                    {{ profile_form.location }}
                    {{ profile_form.location.errors }}
                </div>

                <div class="form-group">
                    <label for="{{ profile_form.birth_date.id_for_label }}">出生日期</label>
                    {{ profile_form.birth_date }}
                    {{ profile_form.birth_date.errors }}
                </div>
            </div>

            <div class="action-buttons">
                <button type="submit" class="btn btn-primary">保存更改</button>
                <a href="{% url 'accounts:profile' %}" class="btn btn-secondary">取消</a>
            </div>
        </form>
    </div>
</body>
</html>